// This MFC Samples source code demonstrates using MFC Microsoft Office Fluent User Interface 
// (the "Fluent UI") and is provided only as referential material to supplement the 
// Microsoft Foundation Classes Reference and related electronic documentation 
// included with the MFC C++ library software.  
// License terms to copy, use or distribute the Fluent UI are available separately.  
// To learn more about our Fluent UI licensing program, please visit 
// http://msdn.microsoft.com/officeui.
//
// Copyright (C) Microsoft Corporation
// All rights reserved.

// SketchDoc.cpp : implementation of the CSketchDoc class
//

#include "stdafx.h"
#include "Sketch.h"

#include "SketchDoc.h"
#include "SrvrItem.h"
#include "TxnMgr.h"
#include "TxnMemory.h"


// CSketchDoc

IMPLEMENT_DYNCREATE(CSketchDoc, COleServerDocEx)

BEGIN_MESSAGE_MAP(CSketchDoc, COleServerDocEx)
END_MESSAGE_MAP()


// CSketchDoc construction/destruction



CSketchDoc::CSketchDoc()
:m_vecGeometries(*txn_new<TxnVector>()) // TODO
{
	m_pTxnMgr = new TxnMgr();

	int n = sizeof(TxnVector);

	int i = m_vecGeometries.size();

	// Use OLE compound files
	EnableCompoundFile();

	// TODO: add one-time construction code here

}

CSketchDoc::~CSketchDoc()
{
	for(TxnVector::iterator it = m_vecGeometries.begin(); it != m_vecGeometries.end(); it++)
	{
		delete (*it);
	}
	txn_delete(&m_vecGeometries);
	delete m_pTxnMgr;

}

BOOL CSketchDoc::OnNewDocument()
{
	if (!COleServerDocEx::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}


// CSketchDoc server implementation

COleServerItem* CSketchDoc::OnGetEmbeddedItem()
{
	// OnGetEmbeddedItem is called by the framework to get the COleServerItem
	//  that is associated with the document.  It is only called when necessary.

	CSketchSrvrItem* pItem = new CSketchSrvrItem(this);
	ASSERT_VALID(pItem);
	return pItem;
}



// CSketchDoc serialization

void CSketchDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar << m_vecGeometries.size();
		TxnVector::iterator cit;
		for (cit = m_vecGeometries.begin(); cit != m_vecGeometries.end(); ++cit)
		{
			ar << (*cit);
		}
	}
	else
	{
		size_t nCount;
		ar >> nCount;

		CObject* pData;
		while (nCount--)
		{
			ar >> pData;
			Geometry2D* pGeom = dynamic_cast<Geometry2D*>(pData);
			m_vecGeometries.push_back(pGeom);
		}
	}
}


// CSketchDoc diagnostics

#ifdef _DEBUG
void CSketchDoc::AssertValid() const
{
	COleServerDocEx::AssertValid();
}

void CSketchDoc::Dump(CDumpContext& dc) const
{
	COleServerDocEx::Dump(dc);
}
#endif //_DEBUG

void CSketchDoc::AddGeometry(Geometry2D* pObj)
{
	ASSERT(pObj);
	m_vecGeometries.push_back(pObj);
}

// CSketchDoc commands
